package com.yfux._05_tlias.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yfux._05_tlias.mapper.EmpMapper;
import com.yfux._05_tlias.pojo.Emp;
import com.yfux._05_tlias.pojo.PageBean;
import com.yfux._05_tlias.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class EmpServiceImpl implements EmpService {

    @Autowired
    EmpMapper empMapper;

    @Override
    public PageBean<Emp> list(String name, Short gender, LocalDate begin, LocalDate end, Integer page, Integer pageSize) {
        log.info("查询分页数据, name={}, gender={}, begin={}, end={}, page={}, pageSize={}",
                name, gender, begin, end, page, pageSize);
        PageHelper.startPage(page, pageSize);
        List<Emp> list = empMapper.list(name, gender, begin, end);
        PageInfo<Emp> pageInfo = new PageInfo<>(list);
        PageBean<Emp> pageBean = new PageBean<>();
        pageBean.setRows(pageInfo.getList());
        pageBean.setTotal((int) pageInfo.getTotal());
        return pageBean;
    }

    @Override
    public Integer delete(List<Integer> ids) {
        return empMapper.delete(ids);
    }

    @Override
    public Integer insert(Emp emp) {
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        log.info("插入员工数据: {}", emp);
        return empMapper.insert(emp);
    }
}
